package com.haitonghop.utils.sqllite.datasource;

import java.util.ArrayList;

import com.haitonghop.data.HaiBLMetalData;
import com.haitonghop.utils.Logger;
import com.haitonghop.utils.sqllite.helper.SQLiteHaiBLMetalDataCachedHelper;
import com.haitonghop.utils.sqllite.helper.SQLiteTopicHaiBLHelper;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class TopicHaiBLDataSource {

	// Database fields
	private SQLiteDatabase database;
	private SQLiteTopicHaiBLHelper dbHelper;

	public TopicHaiBLDataSource(Context context) {
		dbHelper = new SQLiteTopicHaiBLHelper(context);
	}

	public void open() throws SQLException {
		Logger.logMe("open data base");
		database = dbHelper.getWritableDatabase();

		if (!isTableExists(SQLiteTopicHaiBLHelper.TABLE_TOPIC, true)) {
			dbHelper.onCreate(database);
		}
	}

	public void close() {
		if (database != null && database.isOpen()) {
			database.close();
		}
		dbHelper.close();
	}

	public boolean isTableExists(String tableName, boolean openDb) {
		try {
			if (openDb) {
				if (database == null || !database.isOpen()) {
					database = dbHelper.getReadableDatabase();
				}

				if (!database.isReadOnly()) {
					database.close();
					database = dbHelper.getReadableDatabase();
				}
			}

			Cursor cursor = database.rawQuery("select * from '" + tableName + "'", null);
			if (cursor != null) {
				cursor.close();
			}
			return true;
		} catch (Exception e) {

		}
		return false;
	}

	private TopicHaiBlData cursorToTopicHaiBlData(Cursor cursor) {
		TopicHaiBlData object = new TopicHaiBlData();
		object.setId(cursor.getInt(0));
		object.setName(cursor.getString(1));
		return object;
	}
	
	public boolean changeTopicName(int id, String newName){
		if (database == null || (database != null && !database.isOpen())) {
			open();
		}
		
		ContentValues values = new ContentValues();
		values.put(SQLiteTopicHaiBLHelper.COLUMN_NAME, newName);
		boolean done = database.update(SQLiteTopicHaiBLHelper.TABLE_TOPIC, values, SQLiteTopicHaiBLHelper.COLUMN_ID + "=" + id, null) > 0;
		close();		
		return done;
	}
	
	public boolean deletedTopicWithId(int id){
		if (database == null || (database != null && !database.isOpen())) {
			open();
		}
		
		boolean done = database.delete(SQLiteTopicHaiBLHelper.TABLE_TOPIC, SQLiteTopicHaiBLHelper.COLUMN_ID + "=" + id, null) > 0;		
		close();		
		return done;
	}

	public ArrayList<TopicHaiBlData> getAllTopic() {
		if (database == null || (database != null && !database.isOpen())) {
			open();
		}

		Cursor cursor = database.rawQuery("select * from '" + SQLiteTopicHaiBLHelper.TABLE_TOPIC + "'", null);
		ArrayList<TopicHaiBlData> arrayList = new ArrayList<TopicHaiBlData>();
		if (cursor != null) {
			for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
				arrayList.add(cursorToTopicHaiBlData(cursor));
			}
			cursor.deactivate();
			cursor.close();
		}
		close();

		return arrayList;
	}

	@SuppressLint("NewApi")
	public long insertTopic(TopicHaiBlData dataSQLObjects) {
		if (database == null || (database != null && !database.isOpen())) {
			open();
		}

		ContentValues values = new ContentValues();
		values.put(SQLiteTopicHaiBLHelper.COLUMN_NAME, dataSQLObjects.getName());

		long id = database.insertWithOnConflict(SQLiteTopicHaiBLHelper.TABLE_TOPIC, null, values, SQLiteDatabase.CONFLICT_IGNORE);

		database.close();
		database = null;

		close();

		Logger.logMe("Insert TopicHaiBlData done!!! ID >>>" + id);
		return id;
	}
}
